//
//  ZYXLoadingVC.m
//  RuiBeiKang
//
//  Created by iOS开发1 on 2019/1/12.
//  Copyright © 2019年 JW. All rights reserved.
//

#import "ZYXLoadingVC.h"
#import "YCDownloadManager.h"
#import "ZYXLoadingCell.h"

#import "ZYXEmptyCell.h"

#import "ZYXClassNameModel.h"
#import "ZYXLayOutView.h"
@interface ZYXLoadingVC ()
@property (nonatomic, strong) NSMutableArray *cacheVideoList;

@property (assign,nonatomic) BOOL isEmpty;
@property (strong ,nonatomic) ZYXLayOutView *alterView;
///选中删除 视频
@property (strong,nonatomic) NSIndexPath *selectPath;
@end

@implementation ZYXLoadingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //正在下载
      self.view.backgroundColor = [UIColor whiteColor];
 
}
#pragma mark MainUI
-(void)setupViews{
    self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationBar_Height-TabBar_Height-HEIGHT(48));
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"ZYXLoadingCell" bundle:nil] forCellReuseIdentifier:@"ZYXLoadingCell"];
        [self.tableView registerNib:[UINib nibWithNibName:@"ZYXEmptyCell" bundle:nil] forCellReuseIdentifier:@"ZYXEmptyCell"];
    [self.view addSubview:self.tableView];
     [self setupRefresh];
   
}
- (void)getCacheVideoList {
    
    [self.cacheVideoList removeAllObjects];
    [self.cacheVideoList addObjectsFromArray:[YCDownloadManager downloadList]];
    if (self.cacheVideoList.count>0) {
        self.isEmpty = NO;
        NSLog(@"正在下载%@",self.cacheVideoList);
        for (YCDownloadItem *item in self.cacheVideoList) {
            ZYXClassNameDerailModel *model = [ZYXClassNameDerailModel infoWithData:item.extraData];
            NSLog(@"++%@",model.title);
            
        }
    
    }else{
        self.isEmpty = YES;
    }
     [self.tableView reloadData];

}
#pragma mark 刷新
- (void)setupRefresh {
      NSLog(@"shuaxin");
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
 
        [self getCacheVideoList];
        [self endRefresh];
    }];
}

- (void)endRefresh {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}
#pragma mark tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.isEmpty) {
        return 1;
    }else{
       return self.cacheVideoList.count;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.isEmpty) {
        ZYXEmptyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYXEmptyCell"];
        cell.imgView.image = IMG(@"wuzhengzaixiazai");
        cell.titleLabel.text = @"暂无正在下载视频";
        return cell;
    }else{
    ZYXLoadingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYXLoadingCell"];
    YCDownloadItem *item = self.cacheVideoList[indexPath.row];
    cell.item = self.cacheVideoList[indexPath.row];
    item.delegate = cell;
    
    return cell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.isEmpty) {
        return SCREEN_HEIGHT-NavigationBar_Height-TabBar_Height;
    }else{
       return HEIGHT(60);
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.isEmpty) {
     
    }else{
        YCDownloadItem *item = self.cacheVideoList[indexPath.row];
        if (item.downloadStatus == YCDownloadStatusDownloading) {
            [YCDownloadManager pauseDownloadWithItem:item];
        }else if (item.downloadStatus == YCDownloadStatusPaused){
            [YCDownloadManager resumeDownloadWithItem:item];
        }else if (item.downloadStatus == YCDownloadStatusFailed){
            [YCDownloadManager resumeDownloadWithItem:item];
        }else if (item.downloadStatus == YCDownloadStatusWaiting){
            [YCDownloadManager pauseDownloadWithItem:item];
        }else if (item.downloadStatus == YCDownloadStatusFinished){
            
        }
        [self.tableView reloadData];
    }
}
//-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
//    WEAK(weakSelf);
//    if (self.isEmpty) {
//        return nil;
//    }else{
//        // 添加一个删除按钮
//        UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
//            self.selectPath = indexPath;
//            weakSelf.alterView.hidden = NO;
//
//            NSLog(@"点击了删除");
//
//        }];
//        deleteRowAction.backgroundColor = [UIColor colorWithHexString:@"#1cca62"];
//
//        // 将设置好的按钮放到数组中返回
//        return @[deleteRowAction];
//    }
//}
-(void)clcikSureBtn{
    self.alterView.hidden = YES;
    YCDownloadItem *item = self.cacheVideoList[self.selectPath.row];
    [YCDownloadManager stopDownloadWithItem:item];
    [self.cacheVideoList removeObjectAtIndex:self.selectPath.row];
    [self.tableView reloadData];
}
-(void)viewWillAppear:(BOOL)animated{
      [self getCacheVideoList];
}
#pragma mark 懒加载
-(NSMutableArray *)cacheVideoList{
    if (!_cacheVideoList) {
        _cacheVideoList = [NSMutableArray array];
    }
    return _cacheVideoList;
}
-(ZYXLayOutView *)alterView{
    if (!_alterView) {
        _alterView = [[NSBundle mainBundle] loadNibNamed:@"ZYXLayOutView" owner:nil options:nil].lastObject;
        _alterView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        _alterView.titleLabel.text = @"删除视频";
        _alterView.contentLabel.text = @"您确定要删除当前视频吗？";
        [_alterView.sureBtn addTarget:self action:@selector(clcikSureBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _alterView;
}
@end
